package com.example.emoiticon;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

import android.view.Menu;
import android.view.View;
import android.view.Choreographer.FrameCallback;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.emoiticon.adapter.FragmentAdapter;
import com.example.emoiticon.adapter.GifAnimationDrawable;
import com.example.emoiticon.adapter.SessionManager;
import com.viewpagerindicator.TitlePageIndicator;

public class MainActivity extends Activity{
	FragmentAdapter mAdapter;
    ViewPager mPager;
    TitlePageIndicator mIndicator;
    LinearLayout inScrollView;
	int i;
	GifAnimationDrawable big;
	Button bt;
	EditText txtnhap;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 mAdapter = new FragmentAdapter(getSupportFragmentManager());
	        mAdapter.getCurrentFragment();
	        mAdapter.notifyDataSetChanged();
	        mPager = (ViewPager)findViewById(R.id.pager);
	        mPager.setAdapter(mAdapter);

	        mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
	        mPager.getCurrentItem();
	        mIndicator.setViewPager(mPager);
	        inScrollView = (LinearLayout)findViewById(R.id.imglayout);
	        txtnhap=(EditText)findViewById(R.id.txtnhap);
	        bt=(Button)findViewById(R.id.btgui);
	        SessionManager s=new SessionManager(this);
			s.setUserData(0);
			bt.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					   addImageView(inScrollView);
				}
			});
	}

	 private void addImageView(LinearLayout layout){
	    	SessionManager s=new SessionManager(this);
	    	
			int value=s.getUserData();
	        final ImageView imageview = new ImageView(this);
	        try {
	       
	        	   
	        		Toast.makeText(this.getApplicationContext(), Integer.toString(value),Toast.LENGTH_SHORT).show();
					big = new GifAnimationDrawable(this.getApplicationContext().getResources().openRawResource(value));		
					imageview.setImageDrawable(big);	
					big.setVisible(true, true);	
					imageview.post(new Runnable() {
						
						@SuppressLint("NewApi") @Override
						public void run() {
							// TODO Auto-generated method stub
							GifAnimationDrawable gif=(GifAnimationDrawable)imageview.getDrawable();
							gif.start();
						
						  
						}
					});
					imageview.invalidate();
					layout.addView(imageview);				
			       } 
	        	catch (NotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			

	       }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


}
